<template>
  <el-container style="padding: 15px">
    <el-header height="130px">
      <el-form size="small" label-width="70px">
        <el-row type="flex" :gutter="20" align="middle">
          <el-col :span="6">
            <el-form-item label="不动产类型" label-width="85px">
              <el-select v-model="searchAllReportObj.projectId">
                <el-option v-for="item in projectList" :key="item.id" :value="item.id" :label="item.projectName" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目编号">
              <el-input v-model="searchAllReportObj.reportId" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目名称">
              <el-input v-model="searchAllReportObj.reportName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="委托人姓名" label-width="85px">
              <el-input v-model="searchAllReportObj.clientUserName" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row type="flex" :gutter="20" align="middle">
          <el-col :span="6">
            <el-form-item label="产权人">
              <el-input v-model="searchAllReportObj.reportPropertyOwner" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="报告类型">
              <el-select v-model="searchAllReportObj.reportTypeId">
                <el-option :value="1" label="预评" />
                <el-option :value="2" label="正式" />
                <el-option :value="3" label="预转正" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="审核状态">
              <el-select v-model="searchAllReportObj.leafId" multiple>
                <el-option v-for="item in leafList" :key="item.id" :label="item.leafName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" :gutter="20" align="middle" justify="center">
          <el-col :span="4">
            <el-form-item label-width="0px">
              <el-button type="primary" @click="getList">搜索</el-button>
              <el-button @click="clearSearch">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-divider />
    <el-main>
      <el-table v-loading="listLoading" :data="reportList">
        <el-table-column label="项目编号" prop="reportId" width="120px">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="toDetails(scope.row.id)">{{ scope.row.reportId }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" prop="reportName" :formatter="subName" width="300px" />
        <el-table-column label="委托人" prop="clientUserName" />
        <el-table-column label="产权人" prop="reportPropertyOwner" />
        <el-table-column label="审核状态" prop="leafName" />
        <el-table-column label="报告类型" prop="reportTypeName" />
        <el-table-column label="项目类型" prop="projectName" />
        <el-table-column label="操作">
          <template v-slot="">
            <el-dropdown size="small">
              <el-button type="primary" size="small">
                <i class="el-icon-s-tools" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <el-button type="text" size="small">提交审核</el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button type="text" size="small">出具</el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button type="text" size="small">用印申请</el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button type="text" size="small">归档</el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button type="text" size="small">作废</el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button type="text" size="small">转正</el-button>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-main>
  </el-container>
</template>

<script>
import { validObj } from '@/utils/validate'
import { getAllReportList, getReportProjectNoPage, searchAllReportList } from '@/api/report'
import { getAuditStatusNoPage } from '@/api/flow'
import { ellipsisString } from '@/utils'

export default {
  name: 'Index',
  data() {
    return {
      reportList: [],
      leafList: [],
      projectList: [],

      currentPage: 1,
      sizePage: 10,
      totalPage: 0,

      searchAllReportObj: {},

      listLoading: false
    }
  },

  created() {
    this.init()
  },

  methods: {

    init() {
      this.getList()
      this.getSearchFormList()
    },

    handleSizeChange(val) {
      this.sizePage = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    getList() {
      const currentPage = this.currentPage
      const sizePage = this.sizePage
      const searchAllReportObj = this.searchAllReportObj
      this.listLoading = true
      if (validObj(searchAllReportObj)) {
        searchAllReportList(currentPage, sizePage, searchAllReportObj).then(res => {
          this.reportList = res.data.list
          this.totalPage = res.data.total
        }).finally(() => {
          this.listLoading = false
        })
      } else {
        getAllReportList(currentPage, sizePage).then(res => {
          console.log(res)
          this.reportList = res.data.list
          this.totalPage = res.data.total
        }).finally(() => {
          this.listLoading = false
        })
      }
    },

    clearSearch() {
      this.searchAllReportObj = {}
      this.getList()
      // 重置成功提示框
      this.$message({
        message: '重置成功',
        type: 'success'
      })
    },

    toDetails(id) {
      this.$router.push({ path: '/report/details', query: { id: id }})
    },

    getAuditReportLeaf() {
      getAuditStatusNoPage().then(res => {
        this.leafList = res.data.list
      })
    },

    async getSearchFormList() {
      await getAuditStatusNoPage().then(res => {
        this.leafList = res.data.list
      })
      await getReportProjectNoPage().then(res => {
        this.projectList = res.data.list
      })
    },

    subName(row, column, cellValue, index) {
      return ellipsisString(cellValue)
    }
  }
}
</script>

<style scoped>

</style>
